let xftPay = require("../util/xftPay");
Page({
  data: {
    isAddCar: false,
    current: "1",
    showModel: false,
    path: "",
    loading: false,
    paied: false,
    disabled: true,
    prov: {
      list1: ["京", "津", "沪", "渝", "苏", "浙", "豫", "粤", "川", "陕"],
      list2: ["冀", "辽", "吉", "皖", "闽", "鄂", "湘", "鲁", "晋"],
      list3: ["黑", "赣", "贵", "甘", "桂", "琼", "云", "青"],
      list4: ["蒙", "藏", "宁", "新"]
    },
    city: {
      list1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
      list2: ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K"],
      list3: ["L", "M", "N", "P", "Q", "R", "S", "T", "U", "V"],
      list4: ["W", "X", "Y", "Z"]
    },
    keyType: "prov",
    plNum: ["", "", "", "", "", "", "", "+"],
    plateNumber: "",
    query: {},
    carNo: "",
    reqStatus: false,
    scene: {},
    options: "",
    borColor: "dashed"
  },
  onLoad(options) {
    let scene = decodeURIComponent(options.scene);
    console.log(scene);
    if (scene != "undefined") {
      scene = JSON.parse(this.getParam("&" + scene));
      this.setData({ scene, options: options.scene });
    }
  },
  onShow(res) {
    this.setData({
      showModel: false
    });
  },
  getParam(url) {
    var index = url.indexOf("&"); //获取？字符位置
    url = url.substr(index + 1); //截取url参数部分
    var arr = new Array();
    arr = url.split("&"); //将各参数分离
    var str = "{";
    //将参数数组部分转换为JSON数据
    for (var i = 0; i < arr.length; i++) {
      var index1 = arr[i].indexOf("=");
      //判断最后一个参数是否有值
      if (index1 == -1) {
        str += ',"' + arr[i] + '":""';
        continue;
      }
      var key = arr[i].substr(0, index1);
      var val = arr[i].substr(index1 + 1);
      var str1 = ",";
      if (i == 0) {
        str1 = "";
      }
      str += str1 + '"' + key + '":' + '"' + val + '"';
    }

    return str + "}";
  },
  focusItem(e) {
    let { target } = e;
    let { index } = target.dataset;
    let { current, plNum } = this.data;
    // if (plNum[index - 1]) {
    if (index == 1) {
      this.setData({ current: index, keyType: "prov" });
    } else if (index == 2) {
      this.setData({
        current: index,
        keyType: "city"
      });
    } else {
      this.setData({ current: index, keyType: "number" });
    }
    
    if (index == 8 && plNum[7] == "+"){
      this.setData({
        plNum: [plNum[0], plNum[1], plNum[2], plNum[3], plNum[4], plNum[5], plNum[6], ""],
        borColor: "solid"
      });
    }else{
      this.setData({
        plNum: [plNum[0], plNum[1], plNum[2], plNum[3], plNum[4], plNum[5], plNum[6], "+"],
        borColor: "dashed"
      });
    }

    // }
  },
  delPLnum(e) {
    let { current, plNum } = this.data;
    if (plNum[current - 1]) {
      plNum[current - 1] = "";
    } else {
      plNum[current - 2] = "";
      current = current - 1;
    }
    if (current) {
      if (current == 1) {
        this.setData({
          plNum,
          current,
          keyType: "prov"
        });
      } else if (current == 2) {
        this.setData({
          plNum,
          current,
          keyType: "city"
        });
      } else {
        this.setData({ plNum, current });
      }
      this.confiDis();
    }
  },
  showModel(e) {
    let { current, plNum } = this.data;
    if (plNum[7] == "+"){
      plNum[7] = ""
    }
    let plateNumberStr = plNum[0] + plNum[1]  + plNum[2] + plNum[3] + plNum[4] + plNum[5] + plNum[6] + plNum[7];
    var openid = wx.getStorageSync("openid")
    console.log(openid);
    wx.showToast({
      icon: 'loading',
      duration: 1000
    }),
      wx.request({
        url: "https://kuaifu.boshukeji.com/api/userInfo/queryUserStatus",
        data: {
          openid: openid,
          plateNumber: plateNumberStr
        },
        success: res => {
          console.log(plateNumberStr)
          if (res.data.result) {
            console.log(res.data.data)
            var plateNumberList = res.data.data.plateNumberList
            if (plateNumberList.length != 0) {
              plateNumberList = plateNumberList.split(",");
            }
            var extData = {
              appid: res.data.data.appId,
              mch_id: res.data.data.mchId,
              nonce_str: res.data.data.nonceStr,
              sign: res.data.data.sign,
              sign_type: res.data.data.signType,
              sub_appid: res.data.data.subAppId,
              sub_mch_id: res.data.data.subMchId,
              sub_openid: res.data.data.subOpenId,
              plate_number: res.data.data.plateNumber,
              trade_scene: "PARKING"
            }

            console.log(plateNumberList);
            console.log(res.data.data.path);
            console.log(res.data.data.extData);
            console.log(extData);
            this.setData({
              isAddCar: true,
              plateNumberList: plateNumberList,
              extData: extData,
              path: res.data.data.path,
              carNo: plateNumberStr
            });
          }
        }
      })
  },
  hideModel(e) {
    //隐藏弹框
    this.setData({
      showModel: false
    });
  },
  test(e) {
    let { value } = e.target.dataset;
    let { current, plNum } = this.data;
    if (current <= 8) {
      if (!value && value != 0) return;
      if (current == 8 && plNum[7] == "+") return;
      plNum[current - 1] = value;
      if (current == 1) {
        this.setData({
          plNum,
          current: parseInt(current) + 1,
          keyType: "city"
        });
      } else if (current == 2) {
        this.setData({
          plNum,
          current: parseInt(current) + 1,
          keyType: "number"
        });
      } else {
        this.setData({
          plNum,
          current: parseInt(current) + 1
        });
      }
    }
    this.confiDis();
  },
  nono: function () {
    var self = this
    self.setData({
      isAddCar: false,
    });
    wx.request({
      url: 'https://kuaifu.boshukeji.com/api/userCarApi/addCar',
      data: {
        openid: wx.getStorageSync("openid"),
        plateNumber: self.data.carNo
      },
      success: res => {
        console.log(res.data)
        if (res.data.success) {
          wx.redirectTo({
            url: '../index/index'
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.meg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })

  },
  confiDis() {
    let { current, plNum } = this.data;
    if (
      plNum[0] &&
      plNum[1] &&
      plNum[2] !== "" &&
      plNum[3] !== "" &&
      plNum[4] !== "" &&
      plNum[5] !== "" &&
      plNum[6] !== ""
    ) {
      this.setData({ disabled: false });
    } else {
      this.setData({ disabled: true });
    }
  },
  goResult() {
    wx.navigateTo({
      url: "/pages/result/index"
    });
  },
  onShareAppMessage: function (res) {
    let { options } = this.data;
    let url = `/pages/list/index?scene=${options}`;

    return {
      title: "开通无感停车，快速离场，先享后付",
      path: url
      // imageUrl: "../images/banner.png"
    };
  }
});
